package com.sdry.web.controller.hpf;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.sdry.model.hpf.Page;
import com.sdry.model.hpf.SystemProperty;
import com.sdry.service.hpf.SystemPropertyService;
import com.sdry.utils.ResponseUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

/**
 * @ClassName: SystemPropertyController 
 * @Description:系统属性Controller
 * @Author hpf
 * @Date 2018年9月25日 下午3:13:52 
 * @version 1.0
 */
@RequestMapping(value="/sysMana")
@Controller
public class SystemPropertyController {

	@Resource
	private SystemPropertyService systemPropertyService;
	
	/**
	 * 分页查询
	 * @param page 分页条件
	 * @param systemProperty 查询条件
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/listPageSystemProperty")
	public void listPageSystemProperty(Page page,SystemProperty systemProperty,HttpServletResponse response) throws Exception{
		Map<String, Object> map = new HashMap<>();
		map.put("limit", page.getLimit());
		map.put("page", page.getPage());
		if (systemProperty.getProperty() != null) {
			map.put("property", systemProperty.getProperty());
		} else {
			map.put("property", "");
			systemProperty.setProperty("");
		}
		int count = systemPropertyService.countSystemProperty(systemProperty);
		List<SystemProperty> systemPropertyList = systemPropertyService.listSystemProperty(map);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		JSONArray jsonArray = JSONArray.fromObject(systemPropertyList, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		ResponseUtil.write(response, jsonObject);
	}
	
	/**
	 * 保存系统属性
	 * @param systemProperty
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/saveSystemProperty")
	public void saveSystemProperty(SystemProperty systemProperty,HttpServletResponse response) throws Exception{
		int isSuccess = 0;
		if (systemProperty.getSysID() == null || "".equals(systemProperty.getSysID())) {
			isSuccess = systemPropertyService.insertSystemProperty(systemProperty);
		} else {
			isSuccess = systemPropertyService.updateSystemProperty(systemProperty);
		}
		ResponseUtil.write(response, isSuccess);
	}
	
	/**
	 * 删除系统属性
	 * @param sysID 系统属性ID
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/deleteSystemProperty")
	public void deleteSystemProperty(Long sysID,HttpServletResponse response) throws Exception{
		int isSuccess = systemPropertyService.daleteSystemProperty(sysID);
		ResponseUtil.write(response, isSuccess);
	}
	
	/**
	 * 批量删除系统属性
	 * @param idArr 系统属性ID数组
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/deleteSystemPropertyByArr")
	public void deleteSystemPropertyByArr(@RequestParam("idArr[]") String[] idArr,HttpServletResponse response) throws Exception{
		int isSuccess = systemPropertyService.daleteSystemPropertys(idArr);
		ResponseUtil.write(response, isSuccess);
	}
}
